﻿using System;
using System.Collections.Generic;
using Shop.Entity;
using SqlSugar;

namespace Shop.Server
{
    public abstract class BaseServer<TEntity> where TEntity : class, new()
    {
        public SqlSugarClient db;

        public BaseServer()
        {
            db = SqlSugarBase.db;
        }

        public bool Insert<TInsert>(TInsert t, ComUser user) where TInsert : AddEntity, new()
        {
            t.CreateId = user.Id;
            t.CreateName = user.UserName;
            t.IsDel = false;
            t.CreateAt = DateTime.Now;
            return db.Insertable<TInsert>(t).ExecuteCommand() > 0;
        }

        public bool Update<TUpdate>(TUpdate t, ComUser user) where TUpdate : UpdateEntity, new()
        {
            t.UpdateId = user.Id;
            t.UpdateName = user.UserName;
            t.UpdateAt = DateTime.Now;
            return db.Updateable<TUpdate>(t).ExecuteCommand() > 0;
        }

        public bool Delete<TUpdate>(dynamic id, ComUser user, bool isPhysical = false) where TUpdate : UpdateEntity, new()
        {
            try
            {
                db.Ado.BeginTran();
                var isGuid = id is Guid;
                if (isPhysical)
                {
                    if (isGuid)
                        db.Deleteable<TUpdate>().Where(" Id=@Id", new List<SugarParameter> { new SugarParameter("@Id", id) { DbType = System.Data.DbType.Guid } }).ExecuteCommand();
                    else
                        db.Deleteable<TUpdate>().Where(" Id=@Id", new List<SugarParameter> { new SugarParameter("@Id", id) { DbType = System.Data.DbType.Int32 } }).ExecuteCommand();
                }
                else
                {
                    var model = isGuid ? db.Queryable<TUpdate>().Where(" Id=@Id", new List<SugarParameter> { new SugarParameter("@Id", id) { DbType = System.Data.DbType.Guid } }).First() : db.Queryable<TUpdate>().Where(" Id=@Id", new List<SugarParameter> { new SugarParameter("@Id", id) { DbType = System.Data.DbType.Int32 } }).First();
                    model.IsDel = true;
                    model.UpdateAt = DateTime.Now;
                    model.UpdateId = user.Id;
                    model.UpdateName = user.UserName;
                    db.Updateable(model).ExecuteCommand();
                }
                db.Ado.CommitTran();
                return true;
            }
            catch (Exception e)
            {
                db.Ado.RollbackTran();
                return false;
            }
        }

        public bool Delete<TUpdate>(List<dynamic> list, ComUser user, bool isPhysical = false) where TUpdate : UpdateEntity, new()
        {
            try
            {
                db.Ado.BeginTran();
                list.ForEach(id =>
                {
                    var isGuid = id is Guid;
                    if (isPhysical)
                    {
                        if (isGuid)
                            db.Deleteable<TUpdate>().Where(" Id=@Id", new List<SugarParameter> { new SugarParameter("@Id", id) { DbType = System.Data.DbType.Guid } }).ExecuteCommand();
                        else
                            db.Deleteable<TUpdate>().Where(" Id=@Id", new List<SugarParameter> { new SugarParameter("@Id", id) { DbType = System.Data.DbType.Int32 } }).ExecuteCommand();
                    }
                    else
                    {
                        var model = isGuid ? db.Queryable<TUpdate>().Where(" Id=@Id", new List<SugarParameter> { new SugarParameter("@Id", id) { DbType = System.Data.DbType.Guid } }).First() : db.Queryable<TUpdate>().Where(" Id=@Id", new List<SugarParameter> { new SugarParameter("@Id", id) { DbType = System.Data.DbType.Int32 } }).First();
                        model.IsDel = true;
                        model.UpdateAt = DateTime.Now;
                        model.UpdateId = user.Id;
                        model.UpdateName = user.UserName;
                        db.Updateable(model).ExecuteCommand();
                    }
                });
                db.Ado.CommitTran();
                return true;
            }
            catch (Exception e)
            {
                db.Ado.RollbackTran();
                return false;
            }
        }

        public T Find<T>(dynamic id) where T : BaseEntity, new()
        {
            var isGuid = id is Guid;
            return isGuid ? db.Queryable<T>().Where(" Id=@Id", new List<SugarParameter> { new SugarParameter("@Id", id) { DbType = System.Data.DbType.Guid } }).First() : db.Queryable<T>().Where(" Id=@Id", new List<SugarParameter> { new SugarParameter("@Id", id) { DbType = System.Data.DbType.Int32 } }).First();
        }

        public List<T> GetAll<T>() where T : BaseEntity, new()
        {
            return db.Queryable<T>().Where(x => x.IsDel==false).ToList();
        }
    }
}
